namespace JetBrains.ReSharper.Plugins.FSharp.Tests.Features

open JetBrains.ProjectModel
open JetBrains.ReSharper.Plugins.FSharp.Psi.Metadata
open JetBrains.ReSharper.Plugins.FSharp.Psi.Util
open JetBrains.ReSharper.Plugins.FSharp.Tests
open JetBrains.ReSharper.Psi
open JetBrains.ReSharper.Psi.Impl.Reflection2
open JetBrains.ReSharper.Psi.Modules
open JetBrains.ReSharper.Resources.Shell
open NUnit.Framework

[<FSharpTest>]
type FSharpAssemblyAutoOpenTest() =
    inherit FSharpReferencedAssemblyTestBase()

    override x.RelativeTestDataPath = "common/assemblyAutoOpens"

    override x.DoTest(assemblyPsiModule: IAssemblyPsiModule) =
        let solution = assemblyPsiModule.GetSolution()
        let assemblyFileLoader = solution.GetComponent<IPsiAssemblyFileLoader>()
        let autoOpenCache = solution.GetComponent<FSharpAutoOpenCache>()

        use cookie = ReadLockCookie.Create()
        x.ExecuteWithGold(fun writer ->
            let declaredElements =
                FSharpAutoOpenUtil.getAutoOpenModules assemblyFileLoader autoOpenCache assemblyPsiModule.Assembly

            for declaredElement in declaredElements do
                match declaredElement with
                | :? INamespace as ns -> writer.WriteLine(ns.QualifiedName)
                | :? ITypeElement as typeElement -> writer.WriteLine(typeElement.GetClrName())
                | _ -> failwithf "Unexpected element: %O" declaredElement) |> ignore

    [<Test>] member x.FSharpCore() = x.DoTest("FSharp.Core")
